using UnityEngine;
using System.Collections;

//--------------------------------------------------------------------
[System.Serializable]
public class Cannon {
	
	//--------------------
	public float		m_fAngleDegrees;							// angulo de inclinacion del cañon respecto la direccion del link de la weapon
	public float		m_fTimeBetweenBulletActivation;		// tiempo entre una activacion de bala y la siguiente
	public int			m_iNumBullets2Shoot;				// balas que hay que realizar
	//public int			m_iPlaceholderBulletType;			// PLACEHOLDER
	public Bullet		m_bBullet;
	
	//--------------------
	private int			m_iCurrentBulletShoot = 0;				// contador disparos
	private enum State 
	{
		INACTIVE = 0
		, SHOT
		, END_SHOT_SEQUENCE
		, WAITING_END_OF_SHOT
	}
	private State		m_sState = State.SHOT;
	private Transform	m_tGamePlayAreaTransform;
	
	
	//-----------------------------------------------
	public void Start(){
		m_iCurrentBulletShoot 		= 0;
		m_sState					= State.SHOT;
		m_tGamePlayAreaTransform 	= GameObject.Find( "GameplayArea" ).transform;
	}
	
	//-----------------------------------------------
	// Update is called once per frame
	public void Update ( Transform _transform )
	{
		switch( m_sState ){
			case State.INACTIVE:
				break;
				
			case State.END_SHOT_SEQUENCE:
				m_sState = State.INACTIVE;
				break;
				
			case State.SHOT:
				// doy de alta una bullet
			
				Bullet oInstance = Bullet.Instantiate(m_bBullet, _transform.position, _transform.rotation) as Bullet;
				oInstance.transform.Rotate(0,0,-90);
				oInstance.transform.parent = m_tGamePlayAreaTransform;
			
				Debug.Log( "Creating a new bullet with type "+m_bBullet.name );
			
				++m_iCurrentBulletShoot;
			
				if( m_iCurrentBulletShoot >= m_iNumBullets2Shoot )
					m_sState = State.END_SHOT_SEQUENCE;
				else
					// esperamos a que la bala nos deje libres para continuar disparando
					m_sState = State.WAITING_END_OF_SHOT;
				break;
				
			case State.WAITING_END_OF_SHOT:
			
				//si la bala nos deja libres...
				if( true ){
					m_sState = State.SHOT;
				}
				break;
		}
	}
	
}

//--------------------------------------------------------------------
[System.Serializable]
public class Weapon : MonoBehaviour
{
	//--------------------
	public int 			m_iNumCannons;
	private const int 	m_iMaxCannons 				= 15;
	public 				Cannon[] m_arrCannons 		= new Cannon[m_iMaxCannons];
	
	//--------------------
	private enum State 
	{
		INACTIVE = 0,
		SHOOTING
	}
	private State m_sState;
	

	// Use this for initialization
	void Start ()
	{
		m_sState = State.SHOOTING;
		for( int i = 0 ; i < m_iNumCannons ; ++i )
			m_arrCannons[i].Start();
	}

	// Update is called once per frame
	void Update ()
	{
		switch( m_sState ){
		case State.INACTIVE:
				break;
		case State.SHOOTING:
				for( int i = 0 ; i < m_iNumCannons ; ++i )
					m_arrCannons[i].Update( transform );
				break;
		}
	}
}

